home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 July: Mac OS SDK / Dev.CD Jul 99 SDK1.toast / Development Kits / Mac OS / OpenGL 1.0 SDK / Source / Examples / aux / atlantis / swim.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-05-18  |  5.5 KB  |  196 lines  |  [TEXT/CWIE]

  1. /**
  2.  * (c) Copyright 1993, 1994, 1995, 1996 Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED
  4.  * Permission to use, copy, modify, and distribute this software for
  5.  * any purpose and without fee is hereby granted, provided that the above
  6.  * copyright notice appear in all copies and that both the copyright notice
  7.  * and this permission notice appear in supporting documentation, and that
  8.  * the name of Silicon Graphics, Inc. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission.
  11.  *
  12.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  16.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  21.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  *
  25.  * US Government Users Restricted Rights
  26.  * Use, duplication, or disclosure by the Government is subject to
  27.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29.  * clause at DFARS 252.227-7013 and/or in similar or successor
  30.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  31.  * Unpublished-- rights reserved under the copyright laws of the
  32.  * United States.  Contractor/manufacturer is Silicon Graphics,
  33.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  34.  *
  35.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  36.  */
  37. #include <math.h>
  38. #include <stdlib.h>
  39. #include "gl.h"
  40. #include "atlantis.h"
  41.  
  42. void
  43. FishTransform(fishRec * fish)
  44. {
  45.  
  46.     glTranslatef(fish->y, fish->z, -fish->x);
  47.     glRotatef(-fish->psi, 0.0, 1.0, 0.0);
  48.     glRotatef(fish->theta, 1.0, 0.0, 0.0);
  49.     glRotatef(-fish->phi, 0.0, 0.0, 1.0);
  50. }
  51.  
  52. void
  53. WhalePilot(fishRec * fish)
  54. {
  55.     float cos_theta, v;
  56.     
  57.     fish->phi = -20.0;
  58.     fish->theta = 0.0;
  59.     fish->psi -= 0.5;
  60.  
  61.     cos_theta = cos(fish->theta * RRAD);
  62.     v = WHALESPEED * fish->v;
  63.     
  64.     fish->x += v * cos(fish->psi * RRAD) * cos_theta;
  65.     fish->y += v * sin_func((int) fish->psi) * cos_theta;
  66.     fish->z += v * sin_func((int) fish->theta);
  67. }
  68.  
  69. void
  70. SharkPilot(fishRec * fish)
  71. {
  72.     static int sign = 1;
  73.     float X, Y, Z, tpsi, ttheta, thetal, v, cos_theta;
  74.  
  75.     fish->xt = 60000.0;
  76.     fish->yt = 0.0;
  77.     fish->zt = 0.0;
  78.  
  79.     X = fish->xt - fish->x;
  80.     Y = fish->yt - fish->y;
  81.     Z = fish->zt - fish->z;
  82.  
  83.     thetal = fish->theta;
  84.  
  85.     ttheta = RAD * atan(Z / (sqrt(X * X + Y * Y)));
  86.  
  87.     if (ttheta > fish->theta + 0.25) {
  88.         fish->theta += 0.5;
  89.     } else if (ttheta < fish->theta - 0.25) {
  90.         fish->theta -= 0.5;
  91.     }
  92.     if (fish->theta > 90.0) {
  93.         fish->theta = 90.0;
  94.     }
  95.     if (fish->theta < -90.0) {
  96.         fish->theta = -90.0;
  97.     }
  98.     fish->dtheta = fish->theta - thetal;
  99.  
  100.     tpsi = RAD * atan2(Y, X);
  101.  
  102.     fish->attack = 0;
  103.  
  104.     if (fabs(tpsi - fish->psi) < 10.0) {
  105.         fish->attack = 1;
  106.     } else if (fabs(tpsi - fish->psi) < 45.0) {
  107.         if (fish->psi > tpsi) {
  108.             fish->psi -= 0.5;
  109.             if (fish->psi < -180.0) {
  110.                 fish->psi += 360.0;
  111.             }
  112.         } else if (fish->psi < tpsi) {
  113.             fish->psi += 0.5;
  114.             if (fish->psi > 180.0) {
  115.                 fish->psi -= 360.0;
  116.             }
  117.         }
  118.     } else {
  119.         if (rand() % 100 > 98) {
  120.             sign = 1 - sign;
  121.         }
  122.         fish->psi += sign;
  123.         if (fish->psi > 180.0) {
  124.             fish->psi -= 360.0;
  125.         }
  126.         if (fish->psi < -180.0) {
  127.             fish->psi += 360.0;
  128.         }
  129.     }
  130.  
  131.     if (fish->attack) {
  132.         if (fish->v < 1.1) {
  133.             fish->spurt = 1;
  134.         }
  135.         if (fish->spurt) {
  136.             fish->v += 0.2;
  137.         }
  138.         if (fish->v > 5.0) {
  139.             fish->spurt = 0;
  140.         }
  141.         if ((fish->v > 1.0) && (!fish->spurt)) {
  142.             fish->v -= 0.2;
  143.         }
  144.     } else {
  145.         if (!(rand() % 400) && (!fish->spurt)) {
  146.             fish->spurt = 1;
  147.         }
  148.         if (fish->spurt) {
  149.             fish->v += 0.05;
  150.         }
  151.         if (fish->v > 3.0) {
  152.             fish->spurt = 0;
  153.         }
  154.         if ((fish->v > 1.0) && (!fish->spurt)) {
  155.             fish->v -= 0.05;
  156.         }
  157.     }
  158.  
  159.     cos_theta = cos(fish->theta * RRAD);
  160.     v = SHARKSPEED * fish->v;
  161.     
  162.     fish->x += v * cos(fish->psi * RRAD) * cos_theta;
  163.     fish->y += v * sin_func((int) fish->psi) * cos_theta;
  164.     fish->z += v * sin_func((int) fish->theta);
  165. }
  166.  
  167. void
  168. SharkMiss(int i)
  169. {
  170.     int j;
  171.     float avoid, thetal;
  172.     float X, Y, Z, R;
  173.  
  174.     for (j = 0; j < NUM_SHARKS; j++) {
  175.         if (j != i) {
  176.             X = sharks[j].x - sharks[i].x;
  177.             Y = sharks[j].y - sharks[i].y;
  178.             Z = sharks[j].z - sharks[i].z;
  179.  
  180.             R = sqrt(X * X + Y * Y + Z * Z);
  181.  
  182.             avoid = 1.0;
  183.             thetal = sharks[i].theta;
  184.  
  185.             if (R < SHARKSIZE) {
  186.                 if (Z > 0.0) {
  187.                     sharks[i].theta -= avoid;
  188.                 } else {
  189.                     sharks[i].theta += avoid;
  190.                 }
  191.             }
  192.             sharks[i].dtheta += (sharks[i].theta - thetal);
  193.         }
  194.     }
  195. }
  196.